<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{
	public function _initialize(){
		if($_COOKIE["manager_id"] == null){
			$index = new IndexController();
			$index->login();exit;
		}
	}
	
	public function index(){
		
	}
	public function showUser(){
		$usernum = M("User")->count();
		$this->assign("usernum",$usernum);
		$this->display("index");
	}
	public function showErector(){
		$erectorrnum = M("Erector")->where("status = 1")->count();
		$this->assign("erectorrnum",$erectorrnum);
		$this->display("showErector");
	}
	public function showErectorApply(){
		$erectorrnum = M("Erector")->where("status = 0")->count();
		$this->assign("erectorrnum",$erectorrnum);
		$this->display("showErectorApply");
	}
	public function getUser(){
		
		$sql = "select *,(select count(*) from task where user_id =u.id) as orders from user as u order by orders desc";
		$user=M("User")->query($sql);
		echo json_encode($user);
	}
	
	public function getErector(){
		$sql = "select *,(select count(*) from task where user_id =e.user_id ) as orders from erector as e left join user as u on e.user_id = u.id where e.status = 1 order by orders desc";
		$user=M("User")->query($sql);
		echo json_encode($user);
	}
	public function getErectorApply(){
		$sql = "select *,(select count(*) from task where user_id =e.user_id and status = 0) as orders from erector as e left join user as u on e.user_id = u.id where e.status = 0 order by apply_time desc";
		$user=M("User")->query($sql);
		for($i=0;$i<count($user);$i++){
			$user[$i]["apply_time"] = date("Y年m月d日 H时i分",$user[$i]["apply_time"]);
			$user[$i]["idcard_image"] = "<img style='height:300px;' src='/Uploads/".$user[$i]["idcard_image"]."' />";
			$user[$i]["contro"] = "<a href='/Home/User/doneApply?type=ok&id=".$user[$i]['id']."'>通过</a> | <a href='/Home/User/doneApply?type=no&id=".$user[$i]['id']."'>不通过</a>";
		}
		echo json_encode($user);
	}
	public function doneApply(){
		//dump($_REQUEST);exit;
		$id = $_GET["id"];
		$type = $_GET["type"];
		if ($type == "ok"){
			$user = M("User");
			$user->startTrans();
			$user->is_erector = 1;
			if($user->where("id = $id")->save()){
				$erec = M("Erector");
				$erec->status = 1;
				if($erec->where("user_id = $id")->save()){
					$user->commit();
					$this->success("处理完成","/Home/User/showErectorApply",2);
				}else{
					$user->rollback();
					$this->error("处理失败","/Home/User/showErectorApply",2);
				}
			}else{
				$this->error("处理失败","/Home/User/showErectorApply",2);
			}
			
		}elseif($type == "no"){
			$erec = M("Erector");
			$erec->status = -1;
			if($erec->where("user_id = $id")->save()){
			
				$this->success("处理完成","/Home/User/showErectorApply",2);
			}else{
				
				$this->error("处理失败","/Home/User/showErectorApply",2);
			}
			
		}
	}
	
}